package com.project.dao.impl;

import com.project.bean.CutPageBean;
import com.project.bean.OrderBean;
import com.project.dao.IOrderDAO;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;

@Repository
public class OrderDAOImpl extends BaseDAO implements IOrderDAO {
    @Override
    public CutPageBean<OrderBean> cutAll(int pageNO) {
        CutPageBean<OrderBean> cutPageBean = new CutPageBean<>();
        this.getConnection();
        try {
            ps = con.prepareStatement("select * from t_order limit ?,?");
            ps.setObject(1,(pageNO-1)*cutPageBean.PAGESIZE);
            ps.setObject(2,cutPageBean.PAGESIZE);
            rs = ps.executeQuery();
            while (rs.next()){
                cutPageBean.getList().add(this.getOrderBean(rs));
            };
            ps = con.prepareStatement("select count(*) c from t_order");
            rs = ps.executeQuery();
            if (rs.next()){
                cutPageBean.setCount(rs.getInt("c"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.close();
        }
        return cutPageBean;
    }

    private OrderBean getOrderBean(ResultSet rs) throws SQLException {
        OrderBean orderBean = new OrderBean();
        orderBean.setId(rs.getInt("pk_id"));
        orderBean.setUserName(rs.getString("o_username"));
        orderBean.setOrderDate(rs.getString("o_orderDate")!=null?LocalDate.parse(rs.getString("o_orderDate")):null);
        orderBean.setAddress(rs.getString("o_address"));
        return orderBean;
    }

    @Override
    public void add(OrderBean order) {
        this.getConnection();
        try {
            ps = con.prepareStatement("insert into t_order(o_username,o_orderDate,o_address)" +
                    "values (?,?,?)");
            ps.setObject(1,order.getUserName());
            ps.setObject(2,order.getOrderDate());
            ps.setObject(3,order.getAddress());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.close();
        }
    }

    @Override
    public void del(int id) {
        this.getConnection();
        try {
            ps = con.prepareStatement("delete from t_order where pk_id=?");
            ps.setObject(1,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.close();
        }
    }

    @Override
    public void update(int id, String address) {
        this.getConnection();
        try {
            ps = con.prepareStatement("update t_order set o_address=? where pk_id=?");
            ps.setObject(1,address);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.close();
        }
    }

    @Override
    public OrderBean findById(int id) {
        OrderBean orderBean = null;
        this.getConnection();
        try {
            ps = con.prepareStatement("select * from t_order where pk_id=?");
            ps.setObject(1,id);
            rs = ps.executeQuery();
            if (rs.next()){
                orderBean = this.getOrderBean(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.close();
        }
        return orderBean;
    }
}
